namespace Billing
{
    using AjaxControlToolkit;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Security.Application;

    public class ucGroupMemberInPermission : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnDelete;
        protected ConfirmButtonExtender btnDelete_ConfirmButtonExtender;
        protected Button btnReset;
        protected Button btnSave;
        protected CheckBox chkCreate;
        protected CheckBox chkDelete;
        protected CheckBox chkRead;
        protected CheckBox chkUpdate;
        protected DropDownList ddlGroupMember;
        protected DropDownList ddlResourceMenu;
        private DataTable dt;
        protected Panel PanelDetails;
        protected Timer TimerCloseIt;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel3;
        protected UpdateProgress UpdateProgress1;
        protected HiddenField xDialogID;
        protected HiddenField xPrimaryKey;
        protected HiddenField xSecondaryKey;

        private void BindDLLGroupMember()
        {
            DataTable dt = new ClsGroupMember().SelectAll();
            this.ddlGroupMember.Items.Clear();
            this.ddlGroupMember.Items.Add(new ListItem("Select Group Member", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlGroupMember.Items.Add(new ListItem(Encoder.HtmlEncode(dt.Rows[i]["GroupName"].ToString()), Encoder.HtmlEncode(dt.Rows[i]["GroupID"].ToString())));
            }
        }

        private void BindDLLResourceMenu()
        {
            DataTable dt = new ClsResourceMenu().SelectAll();
            this.ddlResourceMenu.Items.Clear();
            this.ddlResourceMenu.Items.Add(new ListItem("Select Resource Menu", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlResourceMenu.Items.Add(new ListItem(Encoder.HtmlEncode(dt.Rows[i]["ResourceName"].ToString()), Encoder.HtmlEncode(dt.Rows[i]["ID"].ToString())));
            }
        }

        public void BindEditData()
        {
            if ((this.dt != null) && (this.dt.Rows.Count > 0))
            {
                this.xPrimaryKey.Value = this.dt.Rows[0]["GroupMemberID"].ToString();
                this.xSecondaryKey.Value = this.dt.Rows[0]["ResourceID"].ToString();
                this.ddlGroupMember.SelectedIndex = -1;
                this.ddlResourceMenu.SelectedIndex = -1;
                this.ddlGroupMember.Items.FindByValue(this.dt.Rows[0]["GroupMemberID"].ToString()).Selected = true;
                this.ddlResourceMenu.Items.FindByValue(this.dt.Rows[0]["ResourceID"].ToString()).Selected = true;
                this.chkCreate.Checked = (bool) this.dt.Rows[0]["Create0"];
                this.chkRead.Checked = (bool) this.dt.Rows[0]["Read0"];
                this.chkUpdate.Checked = (bool) this.dt.Rows[0]["Update0"];
                this.chkDelete.Checked = (bool) this.dt.Rows[0]["Delete0"];
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ClsGroupMemberInResourcePermission cls = new ClsGroupMemberInResourcePermission();
            if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
            {
                cls.GroupMemberID = Convert.ToInt64(this.xPrimaryKey.Value);
                cls.ResourceID = Convert.ToInt64(this.xSecondaryKey.Value);
                cls.Delete();
                ClsUtilityHelper.ResetFormControlValues(this);
                this.TimerCloseIt.Enabled = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlGroupMember.SelectedIndex <= 0)
            {
                this.ucHighlight1.ErrorMessage = "Group Member required";
            }
            else if (this.ddlResourceMenu.SelectedIndex <= 0)
            {
                this.ucHighlight1.ErrorMessage = "Resource Menu required";
            }
            else
            {
                ClsGroupMemberInResourcePermission cls = new ClsGroupMemberInResourcePermission();
                if (this.ddlGroupMember.SelectedIndex != -1)
                {
                    cls.GroupMemberID = Convert.ToInt64(this.ddlGroupMember.SelectedValue);
                }
                if (this.ddlResourceMenu.SelectedIndex != -1)
                {
                    cls.ResourceID = Convert.ToInt64(this.ddlResourceMenu.SelectedValue);
                }
                cls.Create0 = this.chkCreate.Checked;
                cls.Read0 = this.chkRead.Checked;
                cls.Update0 = this.chkUpdate.Checked;
                cls.Delete0 = this.chkDelete.Checked;
                //if (Convert.ToInt64(this.xPrimaryKey.Value) > 0)
                if (cls.Select().Rows.Count>0)
                {
                    cls.ModifiedBy = (long) base.Session["UserID"];
                    cls.Update(Convert.ToInt64(this.xPrimaryKey.Value), Convert.ToInt64(this.xSecondaryKey.Value));
                    this.ucHighlight1.Message = " Record Update ";
                }
                else
                {
                    cls.CreatedBy = (long) base.Session["UserID"];
                    cls.Save();
                    this.ucHighlight1.Message = " Record Saved. ";
                }
                ClsUtilityHelper.ResetFormControlValues(this);
                this.TimerCloseIt.Enabled = true;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindDLLResourceMenu();
                this.BindDLLGroupMember();
            }
            //this.ucHighlight1.CloseAll();
            //this.TimerCloseIt.Enabled = false;
        }

        protected void TimerCloseIt_Tick(object sender, EventArgs e)
        {
            ClsUtilityHelper.CloseJQueryDialog(this.DialogName, this);
            this.TimerCloseIt.Enabled = false;
            ClsUtilityHelper.EnableFormControlValues(this);
            this.ucHighlight1.CloseAll();
        }

        public bool Create0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }

        public bool Delete0
        {
            get
            {
                return this.btnDelete.Visible;
            }
            set
            {
                this.btnDelete.Visible = value;
            }
        }

        public string DialogName
        {
            get
            {
                return this.xDialogID.Value;
            }
            set
            {
                this.xDialogID.Value = value;
            }
        }

        public DataTable EditData
        {
            get
            {
                return this.dt;
            }
            set
            {
                this.dt = value;
                this.BindEditData();
                this.TimerCloseIt.Enabled = false;
            }
        }

        public bool Update0
        {
            get
            {
                return this.btnSave.Visible;
            }
            set
            {
                this.btnSave.Visible = value;
            }
        }
    }
}
